package cn.jiguang.jgadpubadapter.adapter;

import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;


import cn.jiguang.jgadpubadapter.JgPubAdInit;
import cn.jiguang.jgadpubadapter.LogUtil;
import cn.jiguang.jgssp.ad.ADJgSplashAd;
import cn.jiguang.jgssp.ad.data.ADJgSplashAdInfo;
import cn.jiguang.jgssp.ad.entity.ADJgAdSize;
import cn.jiguang.jgssp.ad.entity.ADJgExtraParams;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgSplashAdListener;
import xyz.adscope.amps.ad.splash.adapter.AMPSSplashAdAdapterListener;
import xyz.adscope.amps.ad.splash.adapter.AMPSSplashAdapter;
import xyz.adscope.amps.common.AMPSError;
import xyz.adscope.amps.init.inter.IAMPSChannelInitCallBack;
import xyz.adscope.amps.inner.AMPSAdBiddingListener;
import xyz.adscope.amps.model.AMPSAdapterModel;
import xyz.adscope.amps.tool.util.AMPSScreenUtil;

public class JgPuAdSplashAdapter extends AMPSSplashAdapter {
    ADJgSplashAd adJgSplashAd;

    @Override
    public boolean isValid() {
        if (adJgSplashAd == null) {
            return true;
        }
        return false;
    }

    @Override
    public void startBid(Context context, AMPSAdapterModel ampsAdapterModel,
                         AMPSAdBiddingListener listener) {
        super.startBid(context, ampsAdapterModel, listener);
        initSDK();
    }

    @Override
    public void loadNetworkAd(Context context, AMPSAdapterModel ampsAdapterModel, AMPSSplashAdAdapterListener listener) {
        super.loadNetworkAd(context, ampsAdapterModel, listener);
        //实现竞价逻辑时需要判断是否是bidding广告，如果是bidding广告，如果已经发起过请求直接回调广告请求成功
        if (isBidding && adJgSplashAd != null) {
            JgPuAdSplashAdapter.this.onAdLoad();
            return;
        }
        initSDK();
    }

    private void initSDK() {
        JgPubAdInit.getInstance().initSDK(mInitAdapterConfig, new IAMPSChannelInitCallBack() {
            @Override
            public void successCallBack() {
                loadAd();
            }

            @Override
            public void failCallBack(AMPSError ampsError) {
                JgPuAdSplashAdapter.this.onAdFailed(ampsError.getCode(), ampsError.getMessage());
            }
        });
    }

    private void loadAd() {
        ADJgExtraParams extraParams = new ADJgExtraParams.Builder()
                .adSize(new ADJgAdSize(AMPSScreenUtil.getScreenWidth(mContext), AMPSScreenUtil.getScreenHeight(mContext)))
                .build();
        adJgSplashAd = new ADJgSplashAd(mContext);
        adJgSplashAd.setLocalExtraParams(extraParams);

        adJgSplashAd.setListener(new ADJgSplashAdListener<ADJgSplashAdInfo>() {
            @Override
            public void onADTick(long l) {


            }

            @Override
            public void onReward(ADJgSplashAdInfo adJgSplashAdInfo) {

            }

            @Override
            public void onAdSkip(ADJgSplashAdInfo adJgSplashAdInfo) {
                LogUtil.d(TAG, "onAdSkip: " + adJgSplashAdInfo);
                JgPuAdSplashAdapter.this.onAdDismiss();
            }

            @Override
            public void onAdReceive(ADJgSplashAdInfo adJgSplashAdInfo) {
                LogUtil.d(TAG, "onAdReceive: " + adJgSplashAdInfo);
                if (isBidding) {
                    int mEcpm = (int) adJgSplashAdInfo.getECPM();
                    JgPuAdSplashAdapter.this.onC2SBiddingSuccess(mEcpm);
                } else {
                    JgPuAdSplashAdapter.this.onAdLoad();
                }
            }

            @Override
            public void onAdExpose(ADJgSplashAdInfo adJgSplashAdInfo) {
                LogUtil.d(TAG, "onAdExpose: " + adJgSplashAdInfo);
                JgPuAdSplashAdapter.this.onAdShow();
            }

            @Override
            public void onAdClick(ADJgSplashAdInfo adJgSplashAdInfo) {
                LogUtil.d(TAG, "onAdClick: " + adJgSplashAdInfo);
                JgPuAdSplashAdapter.this.onAdClicked();
            }

            @Override
            public void onAdClose(ADJgSplashAdInfo adJgSplashAdInfo) {
                LogUtil.d(TAG, "onAdClose: " + adJgSplashAdInfo);
                JgPuAdSplashAdapter.this.onAdDismiss();
            }

            @Override
            public void onAdFailed(ADJgError adJgError) {
                LogUtil.d(TAG, "onAdFailed: " + adJgError);
            }
        });
        adJgSplashAd.loadOnly(mSpaceId);
    }

    private static final String TAG = "JgPuAdSplashAdapter";

    @Override
    public void showAd(ViewGroup viewGroup) {
        LogUtil.d(TAG, "showAd: " + viewGroup.getId());
        adJgSplashAd.showSplash(viewGroup);
    }
}
